

<html>
    <head>
        <meta charset="utf-8">
        <title>CONTACTS</title> 
        <link rel="stylesheet" href="../bootstrap/css/bootstrap.css"/>  
    </head>
    <body>

        <?php
        if (isset($_GET["id"])) {
            $id = $_GET["id"];
            $sql = "SELECT * FROM  entreprises, contacts WHERE entreprises.id = contacts.id_entreprise AND id_entreprise='$id' ";
        } elseif (isset($_GET["tous"])) {
            $sql = "SELECT * FROM entreprises, contacts WHERE entreprises.id = contacts.id_entreprise";
        } elseif (isset($_GET["croissant"])) {
            $sql = "SELECT * FROM entreprises, contacts  WHERE entreprises.id = contacts.id_entreprise ORDER BY nom ASC, prenom ASC";
        } elseif (isset($_GET["decroissant"])) {
            $sql = "SELECT * FROM entreprises, contacts WHERE entreprises.id = contacts.id_entreprise ORDER BY nom DESC, prenom DESC";
        } else {
            $sql = "SELECT * FROM contacts ";
        }

        // Connexion à la base de données

        $host = "localhost";
        $user = "root";
        $password = "toto";
        $database = "carnet_adresses";
        $connexion = mysqli_connect($host, $user, $password, $database);

        if (mysqli_connect_errno()) {
            echo ("Echec de la connexion : Erreur n°" . mysqli_connect_errno() . " / " .
            mysqli_connect_error());
        }
        //  else 
        //       { 
        //            echo "Connexion au serveur MySQL OK !"; 
        //      }
        ?>

        <style>
            h2{
                color : #58ACFA;
            }
        </style>

        <h2>LISTE DES CONTACTS</h2>

        <div id="div2">

<!--            Nombre de contacts affichés :     <span><?php // echo $nbre;  ?></span>
      <br>
            -->
            <form>
                <input class="btn btn-default" type="button" value="Afficher tous les contacts" onclick="window.location.href = '../contacts/liste_contacts.php?tous'">
                <input class="btn btn-default" type="button" value="Afficher tous croissant" onclick="window.location.href = '../contacts/liste_contacts.php?croissant'">
                <input class="btn btn-default" type="button" value="Afficher tous décroissant" onclick="window.location.href = '../contacts/liste_contacts.php?decroissant'">
            </form>
            <br>

        </div>
<?php
// Affecter à $resultats sous forme de tableau associatif
$envoi_requete = mysqli_query($connexion, $sql);
while ($resultats = mysqli_fetch_assoc($envoi_requete)) {
    ?>


            <table class="table table-bordered">

                <tr>
                    <th class = active> N° </th>     
                    <td class = active> <?php echo $resultats["id"] ?> </td>
                </tr>

                <tr>
                    <th> Nom et Prénom </th>            
                    <td> <?php echo $resultats["nom"] . " " . $resultats["prenom"] ?> </td>
                </tr>

                <tr>
                    <th> Entreprise </th>
                    <td>  <?php
        $sql_ent = "SELECT raison_sociale FROM entreprises WHERE id=" . $resultats["id_entreprise"];
        $req_ent = mysqli_query($connexion_bd, $sql_ent);
        $data_ent = mysqli_fetch_assoc($req_ent);

        echo $data_ent["raison_sociale"];
        ?> </td>
                </tr>

                <tr>
                    <th> Téléphone </th>
                    <td> <?php echo $resultats["telephone"] ?> </td>
                </tr>

                <tr>
                    <th> E-mail </th>
                    <td> <?php echo $resultats["email"] ?> </td>           
                </tr>

                <tr>
                    <th>Photo </th>
                    <td> <?php echo $resultats["photo"] ?> </td>
                </tr>

                <tr>
                    <th> Observations </th>
                    <td> <?php echo $resultats["observations"] ?> </td>
                </tr>
            </table>     

    <?php
}
?>
    </body>
</html>

